var ptype = ''; 
var w=null;
var url = '';
var pays = {};
var spid = nums = did = shul = member_id = ws = spage = '';
var money =  zf_money = 0;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	member_id = plus.storage.getItem('id');
	
	 
// 获取支付通道
	plus.payment.getChannels(function(channels){
//		console.log(JSON.stringify(channels));
		for(var i in channels){
			var channel=channels[i];
			pays[channel.id]=channel;
			checkServices(channel);
		}
	},function(e){ 
		outLine("获取支付通道失败："+e.message);
	});
	 
	$('.res_pasw').on('click',function(){
		var pay_type = $("input[type='radio'][name='radio1']:checked").attr('id');
		var pay_ment = $(".pay_ment").val();
		var aa = pay_ment.indexOf("元");
		if(aa >= 0){
			var pay_ment = parseInt(pay_ment);
		}
		var timestamp = new Date().getTime();
//		console.log(timestamp);return; 
//		console.log(order_id+'--'+order_sn+'--'+pay_type+'--'+goods_total);return;
		if(pay_type == 'zfb'){          //微信支付
			ptype = 'wxpay';
			payment(timestamp,pay_ment);
		 
//			        plus.nativeUI.toast('微信支付接口暂未提供，敬请期待！');
		}else if(pay_type == 'let'){   //支付宝支付
			ptype = 'alipay';
			payment(timestamp,pay_ment);
			
			
		}
	})
	
	
},false)  
	      
//支付
function payment(orderid, price){
//	console.log(ordernum+" "+price+" "+ptype);return;
	//预设订单Id ——> 用户Id+当前时间戳
//	orderid = ordernum+new Date().getTime(); 
	if(w){return;}//检查是否请求订单中
	if(ptype == 'alipay'){ 
		console.log(webRoot + '/mobile/api/pay/Alipay.php?orderid=' + orderid + '&price='+price);
		url = webRoot + '/mobile/api/pay/Alipay.php?orderid=' + orderid + '&price='+price;
	}else if(ptype == 'wxpay'){
//		console.log(webRoot + '/pay/index.php?orderid=' + orderid + '&price='+price);
		url = webRoot + '/mobile/api/pay/index.php?orderid=' + orderid + '&price='+price;
	}
	w = plus.nativeUI.showWaiting('支付中...',{width:'80px',height:'80px',background:'rgba(0,0,0,0.3)'});
	// 请求支付订单
	var xhr=new XMLHttpRequest();
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch (xhr.readyState) {
			case 4:
			w.close();  
			w = null;  
			if (xhr.status == 200) { 
				var order = xhr.responseText;
				console.log(order);  
				console.log(JSON.stringify(pays[ptype])); 
				plus.payment.request(pays[ptype], order, function(result) { 
					//对订单进行操作
//					console.log('支付成功');
//					console.log(JSON.stringify(result));
					recharge(orderid,price);
//					paying(price);
				}, function(e) { 
					toast("支付失败");
					console.log("["+e.code+"]："+e.message);
				});
			} else { 
				plus.nativeUI.toast("获取订单信息失败！", null, "捐赠");
			}
			break;
		default:
			break;
		}
	}
	xhr.open('GET', url);
	xhr.send();
}

////余额充值
function recharge(order_id,price){
//	console.log(member_id +"==="+order_id);
	if(ptype == 'wxpay'){
		price_name = '支付宝';
	}else{
		price_name = '微信';
	}
	$.ajax({ 
		type:"post",
		url:apiRoot + "?act=member_buy&op=recharge",
		data:{
			member_id:member_id,
			pdr_sn:order_id,
			price_name:price_name,
			price_type:ptype,
			price:price
		},  
		dataType:'json', 
		success:function(data){
//			console.log(JSON.stringify(data));
			if(data.code == 200){
				toast('充值成功！');
				plus.webview.getWebviewById(spage).reload();
				setTimeout(function(){
					ws.close();
				},1000)
			}else{
				alert(data.datas.error);  
			} 
		},
		error:function(e){
			error();
		}
	});
}
//	
//	
//	
//	
//	
//	

// 检测是否安装支付服务
function checkServices(pc){
	if(!pc.serviceReady){
		var txt=null;
		switch(pc.id){
			case "alipay":
			txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
			break;
			default:
			txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
			break;
		} 
		plus.nativeUI.confirm(txt,function(e){
			if(e.index==0){
				pc.installService();
			}
		},pc.description);
	}
}
 